package com.yuanwushe.blue.onceandroid;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

import com.yuanwushe.blue.function.ActivityController;

/**
 * Created by blue on 2018/5/30.
 */

@SuppressLint("Registered")
public class BaseActivity extends AppCompatActivity {

    private ForceOfflineReceiver mForceOfflineReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityController.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityController.removeActivity(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mForceOfflineReceiver != null) {
            unregisterReceiver(mForceOfflineReceiver);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.yuanwushe.blue.FORCE_OFFLINE");
        mForceOfflineReceiver = new ForceOfflineReceiver();
        registerReceiver(mForceOfflineReceiver, intentFilter);
    }

    class ForceOfflineReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(final Context context, Intent intent) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
            alertDialog.setTitle("提示");
            alertDialog.setMessage("广播，别踢下线了");
            alertDialog.setCancelable(false);
            alertDialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    ActivityController.finishAll();
                    Intent intent1 = new Intent(context, LoginActivity.class);
                    startActivity(intent1);
                }
            });
            alertDialog.show();
        }
    }
}
